home *** CD-ROM | disk | FTP | other *** search
- ; Example 5: Input/Output
-
- ; This example illustrates the use of Input/Output instructions
- ; in the xComputer. The input/output facilities of xComputer
- ; are very limited. Furthermore, unlike all the other aspects
- ; of xComputer's machine language, they are "fudged" (rather
- ; than being simulated on the same step-by-step basis that
- ; a real CPU might use).
-
- ; The input instruction is gtc. Output instructions are
- ; ptc, pti, ptu, and ptb. Note that each output item
- ; appears on a line by itself in the Output list (which
- ; appears when I/O Services are turned on). For more details,
- ; look at the end of the file "READ ME (with instructions!)."
-
- ; To use this program, you should turn on the "Use I/O Services"
- ; option in the Options menu. Then, load and run the program
- ; as usual. While the program is running, you can type characters.
- ; These characters are put in an "input queue". The program
- ; fetches and processes these characters one at a time.
- ; At "Fastest" speed, the input is processed just about as
- ; fast as you can type it. At slower speeds, you can follow
- ; what is going on in detail.
-
- ; The program implements a "calculator" that can do addition
- ; and subtraction. While the program is running, type in
- ; numbers separated by + and - signs. Type = to get the
- ; final sum. Any other input will make the program
- ; output a "?" and reset the sum to 0. In this program,
- ; the computer sits in a loop reading typed characters and
- ; processing them. This method of handling input is
- ; called "polling". Another method that uses "interrupts"
- ; is illustrated in the next example file.
-
- @PC 0 ; Make sure the PC contains 0, the starting location
- ; of the program.
-
- start: lod-c 0 ; Set up the starting state: sum = num = 0,
- sto num ; and op = "+". ("Op" represents a
- sto sum ; pending operation that will be
- lod-c '+ ; applied to sum and num; since sum
- sto op ; is 0, applying "+" will just set
- ; set sum := num.)
-
- loop: gtc ; Get a typed character from the input queue.
- jmz loop ; If no character is available, 0 is returned;
- ; in that case just jump back to "loop"
- ; to get the next character.
- sto ch ; Save the typed character in "ch".
-
- sub-c '+ ; Test the typed character. If it is
- jmz do_op ; +, -, or =, then jump to "do_op".
- lod ch ; If it is a digit between 0 and 9,
- sub-c '- ; jump to "digit". Otherwise, it is
- jmz do_op ; illegal input; jump to "bad".
- lod ch
- sub-c '=
- jmz do_op
- lod ch
- sub-c '0
- jmn bad
- lod ch
- sub-c '9
- jmn digit
- jmz digit
-
- bad: lod-c '? ; Illegal input was typed. Output the
- ptc ; chatacter "?" followed by a space,
- lod-c 32 ; then return the calculator to its
- ptc ; starting state.
- jmp start
-
- digit: lod ch ; A digit was typed.
- sub-c '0 ; Convert the character to the corresponding
- sto ch ; number and put that number back in ch.
- lod num ; Set num := 10*num + ch, effectively adding
- shl ; the typed digit onto the end of num.
- shl ; (10*num is computed by shifting num
- shl ; left three times, giving 8*num, and
- add num ; then adding num twice to that.)
- add num
- add ch
- sto num
- jmp loop ; After processing the input digit, go
- ; back to "loop" to get the next input
- ; character.
-
- do_op: lod num ; A "+", "-", or "=" was typed. Output
- ptu ; the current value of "num", and then
- lod op ; either add "num" to "sum" or subtract
- sub-c '+ ; it from "sum", depending on the
- jmz plus ; pending operation, "op".
- minus: lod sum
- sub num
- sto sum
- jmp fin_op
- plus: lod sum
- add num
- sto sum
- fin_op: lod-c 0 ; After the calculation, "num" is reset to 0
- sto num ; so that the user can start typing the
- lod ch ; next number. "Ch", the operation that
- ptc ; was just input, is printed.
- sub-c '= ; If "ch" is "+" or "-", then it is
- jmz equals ; stored in "op" to become the next
- lod ch ; pending operation.
- sto op
- jmp loop
- equals: lod sum ; If the typed character was "=", then
- ptu ; the final answer, "sum" is output,
- lod-c 32 ; followed by a space (ASCII 32) to
- ptc ; separate computations in the output list.
- jmp start ; Then the calculator is returned to its
- ; starting state to get ready for the
- ; next computation.
-
- op: data ; Pending operation, that is, a "+" or "-"
- ; that will be performed after its second
- ; operand has been entered.
- ch: data ; Holds the character typed most recently
- ; by the user.
- num: data ; Holds a number as itis being typed by
- ; the user.
- sum: data ; Holds the result of any computation
- ; performed so far, while the user is
- ; typing the next number.
-